/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package parsertojaklin;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;

/**
 *
 * @author Юра
 */
public class MySQLconnect {
    Connection conn = null;

    public MySQLconnect(String login, String password, String url ) {
           try
           {
               // url = "jdbc:mysql://localhost/test";
               Class.forName ("com.mysql.jdbc.Driver").newInstance ();
               this.conn = (Connection) DriverManager.getConnection ("jdbc:mysql://"+url, login, password);
               System.out.println ("Database connection established");
               Statement s = (Statement) conn.createStatement ();

               s.executeUpdate ("SET NAMES CP1251");
           }
           catch (Exception e)
           {
               System.out.println( e.toString());
               System.err.println ("Cannot connect to database server");
           }
          
    }

    public Statement getStatem() throws SQLException{
    return (Statement) conn.createStatement ();
    }

    public  void Trunk () throws SQLException {
       Statement s = (Statement) conn.createStatement ();

       s.executeUpdate ("TRUNCATE TABLE  animal");
       System.out.println ("Wares trunkated");
    }

    public void Insert () throws SQLException {
    Statement s = (Statement) conn.createStatement ();
       int count = s.executeUpdate (
                   "INSERT INTO wares (name, category)"
                   + " VALUES"
                   + "('snake', 'reptile'),"
                   + "('frog', 'amphibian'),"
                   + "('tuna', 'fish'),"
                   + "('racoon', 'mammal')");
       s.close ();
       System.out.println (count + " wares were inserted");
    }
    public void TrunkVendors () throws SQLException {
       Statement s = (Statement) conn.createStatement ();

       s.executeUpdate (" TRUNCATE TABLE `vendors` ");
       System.out.println ("Wares trunkated");
    }

    public void InsertVendors (LinkedList<String> vendors) throws SQLException {
    Statement s = (Statement) conn.createStatement ();
    String query =   "INSERT INTO vendors (name_vendor) VALUES";
    String names = "";
    for(String name: vendors)
        names += (names.length()==0)?"('"+name+"')":", ('"+name+"')";
    
    query+=names;
    //System.out.println (query);

    int count = s.executeUpdate (query);

       s.close ();
       System.out.println (count + " wares were inserted");
    }


}
